package cn.yonyong.base.project.restTemplate;

import cn.yonyong.base.project.BaseTest;
import cn.yonyong.base.project.config.resTemplate.HttpsClientRequestFactory;
import cn.yonyong.base.project.mapper.TblBatchappearMapper;
import com.alibaba.fastjson.JSONObject;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Describtion  HTTP/HTTPS  get/post 方法的使用
 * @Author yonyong
 * @Date 2019/9/6 14:51
 * @Version 1.0.0
 **/

public class UseRestTemplate extends BaseTest{      //http与https用法一致，只是restTemplate的定义方式不同

    //http方式 只需要@Autowired private RestTemplate restTemplate或者RestTemplate restTemplate = new RestTemplate();即可
    //https方式 需要加上ssl认证 即 RestTemplate restTemplate = new RestTemplate(new HttpsClientRequestFactory());

    @Autowired
    private TblBatchappearMapper tblBatchappearMapper;
    @Test
    public void test(){     //Https get 参数拼接
        RestTemplate restTemplate = new RestTemplate(new HttpsClientRequestFactory());
        String url = "https://192.168.1.23:8853/hermes/api/searchDevice/";
        String address = url+"13355669999";
        String json = restTemplate.getForObject(address, String.class);
        System.out.println(json);
    }

    @Test
    public void test2(){    //Https get 参数传object
        String address = "https://192.168.1.23:8853/hermes/api/searchDevice/{phone}"; //如果要以传参的形式传递，注意这边的url需要有占位的变量
        RestTemplate restTemplate = new RestTemplate(new HttpsClientRequestFactory());
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("phone", "13296793885");//这是该接口需要的参数
//        String json = restTemplate.getForObject(address,String.class,params);看情况用什么类型的接收，也可以用JSONObject接收
        JSONObject json = restTemplate.getForObject(address,JSONObject.class,params);
        System.out.println(json);
    }

    @Test
    public void test3(){    //Https post 参数传object
        RestTemplate restTemplate = new RestTemplate(new HttpsClientRequestFactory());
        Map<String,Object> map = new HashMap<>();
        map.put("idcard", "123");
        String json2 =restTemplate.postForObject("https://192.168.1.23:8853/hermes/api/updateByIdCard", map, String.class);
        System.out.println(json2);
    }

    @Test
    public  void  test4(){
        RestTemplate restTemplate = new RestTemplate(new HttpsClientRequestFactory());
        MultiValueMap<String, Object> map= new LinkedMultiValueMap<String, Object>();
        map.add("reportType", "A");
        map.add("cmcId", "bc7c877c-dd75-4214-bff7-6a406eb5d742");
        map.add("collectTime",String.valueOf( System.currentTimeMillis()));
        map.add("platform", "03");
        map.add("area", "02");
        map.add("status", "02");
        String url = "https://20.65.3.176:10019/mpscmc/v1/subcmc/platform_info";

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
//        headers.add("Content-Type","application/json");
        HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(map,headers);
        String json = restTemplate.postForObject(url,request,String.class);
        System.out.println(json);
//        JSONObject json = restTemplate.postForObject(url,map,JSONObject.class);
    }

    @Test
    public  void  test5(){  //捕获异常  看控制台报什么错，捕获什么异常  问题解决
        RestTemplate restTemplate = new RestTemplate(new HttpsClientRequestFactory());

        Map<String,Object> map = new HashMap<>();
        map.put("reportType", "A");
        map.put("cmcId", "bc7c877c-dd75-4214-bff7-6a406eb5d742");
        map.put("collectTime", String.valueOf(System.currentTimeMillis()));
        map.put("platform", "03");
        map.put("area", "02");
        map.put("status", "02");
        String url = "https://20.65.3.176:10019/mpscmc/v1/subcmc/platform_info";

        restTemplate.setErrorHandler(new DefaultResponseErrorHandler(){
            public void handleEoor(ClientHttpResponse response) throws IOException{
                if (response.getStatusCode() != HttpStatus.INTERNAL_SERVER_ERROR){
                    super.handleError(response);
                }
            }
        });

        String json = null;
        try{
            json = restTemplate.postForObject(url,map,String.class);
        }catch (HttpServerErrorException e){
            json = e.getResponseBodyAsString();
        }
        System.out.println("json--------"+json);
    }

    @Test
    public  void  test6(){
        HttpsClientRequestFactory httpsClientRequestFactory = new HttpsClientRequestFactory();
        httpsClientRequestFactory.setConnectTimeout(10000);
        httpsClientRequestFactory.setReadTimeout(1000);
        RestTemplate restTemplate = new RestTemplate(httpsClientRequestFactory);

        Map<String,Object> map = new HashMap<>();
        map.put("reportType", "A");
        map.put("cmcId", "bc7c877c-dd75-4214-bff7-6a406eb5d742");
        map.put("collectTime", String.valueOf(System.currentTimeMillis()));
        map.put("platform", "03");
        map.put("area", "02");
        map.put("status", "02");
        String url = "https://20.65.3.176:10019/mpscmc/v1/subcmc/platform_info";

        restTemplate.postForObject(url,map,Object.class);
    }

    @Test
    public void sada(){
        String date = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        System.out.println(date);
    }
}
